package org.mogu.control;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.mogu.R;
import org.mogu.Welcome;
import org.mogu.service.GetSyncUrl;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.Imgtools;
import org.mogu.tools.UrlSync;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Weibo10 extends RelativeLayout {
	TextView weibocontent;
	EditText edittext;
	Button cancelbtn;
	Button replaybtn;
	Button sendbtn;
	Button imgbtn;
	Weibo10 weibocontent10;
	String code;
	String replayType;
	String maincode;
	String content;
	Welcome welcome;
	
	ArrayList<String> appMap;

	DefaultHttpClient client;
	HttpResponse httpResponse;

	public Weibo10(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Weibo10(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.weibo10, this, true);
		weibocontent = (TextView) findViewById(R.id.weibo10content);
		edittext = (EditText) findViewById(R.id.weibo10text);
//		imgbtn = (Button) findViewById(R.id.imgbtn);
//		imgbtn.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				welcome.gotoImageGroup();
//			}
//		});
		cancelbtn = (Button) findViewById(R.id.weibo_cancel);
		cancelbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				welcome.goBack();
			}
		});
		sendbtn = (Button) findViewById(R.id.weibo_resend);
		sendbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				try {
					String url;
					String urlparam;
					url = GetSyncUrl.getAppUrlByCode(maincode) + "/replayWeibo";
					urlparam="&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+URLEncoder.encode(getEditText(),"UTF-8")+"&RContent="+URLEncoder.encode(content,"UTF-8");
					UrlSync urlSync=new UrlSync();
					urlSync.setNotice(false);
					urlSync.setUri(url);
					urlSync.setUrlparam(urlparam);
					urlSync.setMaincode(maincode);
					urlSync.setCode(code);
					urlSync.setResultType(replayType);
					urlSync.setToastContentFa("转发失败。");
					urlSync.setToastContentSu("转发成功。");
					synchronized (Convert.class) {
						Convert.syncList.add(urlSync);
					}
					welcome.goBack();
					Toast.makeText(welcome, "正在提交", 4000).show();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
//				new Thread() {
//					public void run() {
//				try {
//					client = new DefaultHttpClient();
//					String url;
//					url = AppUrlMap.getUrl(appMap) + "/replayWeibo?UserName="
//							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+URLEncoder.encode(getEditText(),"UTF-8")+"&RContent="+URLEncoder.encode(content,"UTF-8")
//							+AppUrlMap.getParamByCode(Convert.uname, maincode, DBhelper.getDBHelper(welcome).getReadableDatabase());
//					HttpGet getMethod = new HttpGet(url+ Convert.testNum);
////					welcome.initDialog(null, null);
////					welcome.myDialog.show();
//					httpResponse = client.execute(getMethod);
//
//					if (httpResponse.getStatusLine().getStatusCode() == 200) {
//						// 取出回应字串
//						String strResult;
//						strResult = EntityUtils.toString(httpResponse
//								.getEntity());
//						if ("1".equals(strResult)) {
////							Toast.makeText(welcome, "转发失败，稍后再试。", 4000).show();
//						}else {
////							Toast.makeText(welcome, "转发成功。", 4000).show();
//							InfoSync infoSync=new InfoSync();
//							infoSync.syncw(welcome, DBhelper.getDBHelper(welcome), strResult.split(";"));
//						}
//					}else{
////						Toast.makeText(welcome, "转发失败，稍后再试。", 4000).show();
//					}
//				} catch (Exception e1) {
//					Log.e("转发", "转发失败");
////					welcome.myDialog.dismiss();
//				}finally{
////					welcome.goBack();
//				}
//					}}.start();
					
				
			}
		});
		replaybtn = (Button) findViewById(R.id.weibo_replay);
		replaybtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					String url;
					String urlparam;
					url = GetSyncUrl.getAppUrlByCode(maincode) + "/commitWeibo";
					urlparam="&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+URLEncoder.encode(getEditText(),"UTF-8");
					UrlSync urlSync=new UrlSync();
					urlSync.setNotice(false);
					urlSync.setUri(url);
					urlSync.setUrlparam(urlparam);
					urlSync.setMaincode(maincode);
					urlSync.setCode(code);
					urlSync.setResultType(replayType);
					urlSync.setToastContentFa("评论失败。");
					urlSync.setToastContentSu("评论成功。");
					synchronized (Convert.class) {
						Convert.syncList.add(urlSync);
					}
					welcome.goBack();
					Toast.makeText(welcome, "正在提交", 4000).show();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				new Thread() {
//					public void run() {
//				try {
//					client = new DefaultHttpClient();
//					String url;
//					url = AppUrlMap.getUrl(appMap) + "/commitWeibo?UserName="
//							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content="+URLEncoder.encode(getEditText(),"UTF-8")
//							+AppUrlMap.getParamByCode(Convert.uname, maincode, DBhelper.getDBHelper(welcome).getReadableDatabase());
//					HttpGet getMethod = new HttpGet(url+ Convert.testNum);
////					welcome.initDialog(null, null);
////					welcome.myDialog.show();
//					httpResponse = client.execute(getMethod);
//
//					if (httpResponse.getStatusLine().getStatusCode() == 200) {
//						// 取出回应字串
//						String strResult;
//						strResult = EntityUtils.toString(httpResponse
//								.getEntity());
//						if ("1".equals(strResult)) {
////							Toast.makeText(welcome, "评论失败，稍后再试。", 4000).show();
//						}else{
////							Toast.makeText(welcome, "评论成功。", 4000).show();
//							InfoSync infoSync=new InfoSync();
//							infoSync.syncw(welcome, DBhelper.getDBHelper(welcome), strResult.split(";"));
//						}
//					}else{
////						Toast.makeText(welcome, "评论失败，稍后再试。", 4000).show();
//					}
//				} catch (IOException e1) {
//					Log.e("评论", "评论失败");
//					// TODO Auto-generated catch block
////					welcome.myDialog.dismiss();
//				}finally{
////					welcome.goBack();
//				}
//			}}.start();
			welcome.goBack();
			Toast.makeText(welcome, "提交成功", 4000).show();
			}
		});
	}

	public void setContent(String code, String replayType, String maincode,
			 String content) {
		weibocontent.setText(Imgtools.getContent(content,weibocontent));
		this.code = code;
		this.content = content;
		this.replayType = replayType;
		this.maincode = maincode;
		edittext.setText("");
	}

	public String getEditText() {
		return edittext.getText().toString();
	}

	public void setWeibo10(Weibo10 content, Welcome welcome) {
		weibocontent10 = content;
		this.welcome = welcome;
//		appMap=AppUrlMap.getAppUrlByCode(Convert.uname, this.maincode, DBhelper.getDBHelper(this.welcome).getReadableDatabase());
	}

}
